﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pttk_htt_hr.Utility
{
    class DictionaryUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="LstSqlparam">List phải có @</param>
        /// <param name="ParameterNames">Bắt buộc có @</param>
        /// <returns></returns>
        public static Dictionary<string, string> Extract_ParamVal_In_ListSqlparameter(List<SqlParameter> LstSqlparam, string[] ParameterNames)
        {
            Dictionary<string, string> MatchedKeys = new Dictionary<string, string>();

            foreach (var item in ParameterNames)
            {
                string ParamVal = SqlParameterUitl.Find_ParameterName_In_ListSqlparameter(LstSqlparam, item);
                MatchedKeys.Add(item, ParamVal);
            }

            return MatchedKeys;
        }

        public static Dictionary<string, string> Extract_ParamVal_By_ParamName_From_Dictionary(Dictionary<string, string> Dict, string[] ParameterNames)
        {
            Dictionary<string, string> MatchedKeys = new Dictionary<string, string>();

            foreach (var item in ParameterNames)
            {
                string key = item.Replace("@","");
                string val = Get_ParamVal_By_ParamName_In_Dictionary(Dict, item);

                MatchedKeys.Add(key, val);
            }

            return MatchedKeys;
        }

        public static string Get_ParamVal_By_ParamName_In_Dictionary(Dictionary<string, string> Dict, string DictKey)
        {
            string ParamVal = "";

            string[] keys = Dict.Keys.ToArray();

            for (int i = 0; i < keys.Length; i++)
			{
                string key = keys[i];

                string val1 = Dict[key];
                string val2 = Dict[DictKey.Replace("@","")];

			    if ( val1.Equals(val2))
                {
                    ParamVal = val1;
                    break;
                }
			}
                
            return ParamVal;
        }

        public static Dictionary<string, string> Remove_Amoc_In_Dictionary(Dictionary<string, string> LstObj)
        {
            Dictionary<string, string> Dic = new Dictionary<string, string>();

            string[] keys = LstObj.Keys.ToArray();
            foreach (var item in keys)
            {
                string key = item;
                key = key.Replace("@", "");
                Dic.Add(key, LstObj[item].ToString());
            }

            return Dic;
        }

        public static Dictionary<string, string> Convert_ListSqlparameter_To_Dictionary(List<SqlParameter> LstSqlparam)
        {
            Dictionary<string, string> res = new Dictionary<string, string>();


            foreach (var item in LstSqlparam)
            {
                string key = item.ParameterName.Replace("@","");
                string val = item.Value.ToString();

                res.Add(key, val);
            }
            
            return res;
        }

        /// <summary>
        /// Chỉ convert được khi PHẦN TỬ trong List<ArrayList> là chỉ 1 obj
        /// </summary>
        /// <param name="LstArrLst"></param>
        /// <returns></returns>
        public static List<Dictionary<string, string>> Convert_ListAraryList_To_Dictionary(List<ArrayList> LstArrLst, string key)
        {
            List<Dictionary<string, string>> res = new List<Dictionary<string, string>>();

            for (int i = 0; i < LstArrLst.Count; i++)
			{
                Dictionary<string, string> dic = null;
                if (LstArrLst[i].Count == 1)
                {
                    dic = new Dictionary<string, string>();
                    dic.Add(key, LstArrLst[i][i].ToString());    
                }

                res.Add(dic);
			}
             
            return res;
        }

        public static string Find_Diff_Key_TowDic(Dictionary<string, string> dic1, Dictionary<string, string> dic2)
        {
            string diffKey = "";

            string[] key1 = dic1.Keys.ToArray();
            string[] key2 = dic2.Keys.ToArray();

            if (dic1.Count >= dic2.Count)
            {
                for (int i = 0; i < key1.Length; i++)
                {
                    string key = key1[i];

                    foreach (var item in key2)
                    {
                        if (!item.Equals(key))
                        {
                            diffKey = key;
                        }
                    }
                }
            }

            if (dic2.Count >= dic1.Count)
            {
                for (int i = 0; i < key2.Length; i++)
                {
                    string key = key2[i];

                    foreach (var item in key1)
                    {
                        if (!item.Equals(key))
                        {
                            diffKey = key;
                        }
                    }
                }
            }

            return diffKey;
        }
    }
}
